﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Card.Common
{
    public static class SettingCache
    {
        private static DataTable m_DtCardSetting;
        public static DataTable DtCardSetting
        {
            get
            {
                DataRow[] rows = m_DtCardSetting.Select(string.Format("AREA='{0}'", CardWebUtility.GetAreaCode()));
                if (rows == null || rows.Length <= 0)
                {
                    rows = m_DtCardSetting.Select("AREA='A'");
                }
                return rows.CopyToDataTable();
            }
        }
        private static DataTable m_DtCornerSetting;
        public static DataTable DtCornerSetting
        {
            get
            {
                DataRow[] rows = m_DtCornerSetting.Select(string.Format("AREA='{0}'", CardWebUtility.GetAreaCode()));
                if (rows == null || rows.Length <= 0)
                {
                    rows = m_DtCornerSetting.Select("AREA='A'");
                }
                return rows.CopyToDataTable();
            }
        }
        private static DataTable m_DtPriceSetting;
        public static DataTable DtPriceSetting
        {
            get
            {
                DataRow[] rows = m_DtPriceSetting.Select(string.Format("AREA='{0}'", CardWebUtility.GetAreaCode()));
                if (rows == null || rows.Length <= 0)
                {
                    rows = m_DtPriceSetting.Select("AREA='A'");
                }
                return rows.CopyToDataTable();
            }
        }
        private static DataTable m_DtSiteSetting;

        public static DataTable DtSiteSetting
        {
            get
            {

                DataRow[] rows = m_DtSiteSetting.Select(string.Format("SITE_CODE='{0}'", CardWebUtility.GetAreaCode()));
                if (rows == null || rows.Length <= 0)
                {
                    rows = m_DtSiteSetting.Select("SITE_CODE='A'");
                }
                return rows.CopyToDataTable();
            }
        }

        static SettingCache()
        {
            Refresh();
        }

        public static void Refresh()
        {
            m_DtCardSetting = BLL.CardSettingManager.GetList(" 1=1 ORDER BY SORT", null).Tables[0];
            m_DtCornerSetting = BLL.CornerSettingManager.GetList(" 1=1 ORDER BY SORT", null).Tables[0];
            m_DtPriceSetting = BLL.CardPriceSettingManager.GetList(" 1=1 ORDER BY SORT", null).Tables[0];
            m_DtSiteSetting = BLL.SiteSettingManager.GetList(string.Empty, null).Tables[0];
        }

        /// <summary>
        /// 得到配置的名片
        /// </summary>
        /// <param name="strCode"></param>
        /// <returns></returns>
        public static Entity.CardSetting GetCardSetting(string strCode)
        {
            var rows = (from row in m_DtCardSetting.AsEnumerable()
                        where string.Compare(row["CARD_CODE"].ToString().Trim(), strCode, true) == 0
                            && string.Compare(row["AREA"].ToString().Trim(), CardWebUtility.GetAreaCode(), true) == 0
                        select row).ToArray();
            if (rows == null || rows.Length <= 0)
            {
                rows = (from row in m_DtCardSetting.AsEnumerable()
                        where string.Compare(row["CARD_CODE"].ToString().Trim(), strCode, true) == 0
                            && string.Compare(row["AREA"].ToString().Trim(), "A", true) == 0
                        select row).ToArray();
            }

            if (rows != null && rows.Length > 0)
            {
                return new Card.Entity.CardSetting()
                {
                    CardCode = rows[0]["CARD_CODE"].ToString().Trim(),
                    Area = rows[0]["AREA"].ToString().Trim(),
                    CardName = rows[0]["CARD_NAME"].ToString().Trim(),
                    Sort = string.IsNullOrEmpty(rows[0]["SORT"].ToString()) ? 0 : int.Parse(rows[0]["SORT"].ToString()),
                    Decription = rows[0]["DECRIPTION"].ToString().Trim(),
                    IsDefault = true
                };
            }
            return null;
        }
        /// <summary>
        /// 得到角
        /// </summary>
        /// <param name="strCode"></param>
        /// <returns></returns>
        public static Entity.CornerSetting GetCornerSetting(string strCode)
        {
            var rows = from row in m_DtCornerSetting.AsEnumerable()
                        where string.Compare(row["CORNER_CODE"].ToString().Trim(), strCode, true) == 0
                            && string.Compare(row["AREA"].ToString().Trim(), CardWebUtility.GetAreaCode(), true) == 0
                        select row;
           
            if (rows == null || rows.Count() <= 0)
            {
                rows = from row in m_DtCornerSetting.AsEnumerable()
                        where string.Compare(row["CORNER_CODE"].ToString().Trim(), strCode, true) == 0
                            && string.Compare(row["AREA"].ToString().Trim(), "A", true) == 0
                        select row;
            }
            if (rows != null && rows.Count() > 0)
            {
                return new Card.Entity.CornerSetting()
                {
                    CornerCode = rows.First()["CORNER_CODE"].ToString().Trim(),
                    Area = rows.First()["AREA"].ToString().Trim(),
                    CornerName = rows.First()["CORNER_NAME"].ToString().Trim(),
                    Sort = string.IsNullOrEmpty(rows.First()["SORT"].ToString()) ? 0 : int.Parse(rows.First()["SORT"].ToString()),
                    Price = decimal.Parse(rows.First()["PRICE"].ToString().Trim()),
                    IsDefault = true
                };
            }
            return null;
        }
        /// <summary>
        /// 得到名片的价格
        /// </summary>
        /// <param name="cardCode"></param>
        /// <param name="num"></param>
        /// <returns></returns>
        public static Entity.CardPriceSetting GetPriceSetting(string cardCode, int num)
        {
            var rows = from row in m_DtPriceSetting.AsEnumerable()
                        where string.Compare(row["CARD_CODE"].ToString().Trim(), cardCode, true) == 0
                            && int.Parse(row["NUM"].ToString()) == num
                            && string.Compare(row["AREA"].ToString().Trim(), CardWebUtility.GetAreaCode(), true) == 0
                        select row;

            if (rows == null || rows.Count() <= 0)
            {
                rows = from row in m_DtPriceSetting.AsEnumerable()
                        where string.Compare(row["CARD_CODE"].ToString().Trim(), cardCode, true) == 0
                            && int.Parse(row["NUM"].ToString()) == num
                            && string.Compare(row["AREA"].ToString().Trim(), "A", true) == 0
                        select row;
            }

            if (rows != null && rows.Count() > 0)
            {
                return new Card.Entity.CardPriceSetting()
                {
                    CardCode = rows.First()["CARD_CODE"].ToString().Trim(),
                    Area = rows.First()["AREA"].ToString().Trim(),
                    Num = int.Parse(rows.First()["NUM"].ToString().Trim()),
                    Sort = string.IsNullOrEmpty(rows.First()["SORT"].ToString()) ? 0 : int.Parse(rows.First()["SORT"].ToString()),
                    Price = decimal.Parse(rows.First()["PRICE"].ToString().Trim()),
                    IsDefault = true
                };
            }
            return null;
        }

        public static Entity.SiteSetting GetSiteSettingByType(string strSettingType)
        {
            var rows = from row in m_DtSiteSetting.AsEnumerable()
                       where string.Compare(row["SETTING_TYPE"].ToString().Trim(), strSettingType, true) == 0
                           && string.Compare(row["SITE_CODE"].ToString().Trim(), CardWebUtility.GetAreaCode(), true) == 0
                       select row;
            if (rows == null || rows.Count() <= 0)
            {
                rows = from row in m_DtSiteSetting.AsEnumerable()
                       where string.Compare(row["SETTING_TYPE"].ToString().Trim(), strSettingType, true) == 0
                           && string.Compare(row["SITE_CODE"].ToString().Trim(), "A", true) == 0
                       select row;
            }
            if (rows != null && rows.Count() > 0)
            {
                return new Card.Entity.SiteSetting()
                {
                    
                    SettingType = rows.First()["SETTING_TYPE"].ToString(),
                    Value = rows.First()["VALUE"].ToString(),
                };
            }

            return null;
        }

    }
}
